package com.otitan.la.forest.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.otitan.la.forest.system.dto.ApplicationDTO;
import com.otitan.la.forest.system.entity.ApplicationEntity;
import com.otitan.la.forest.system.service.ApplicationService;
import com.otitan.la.forest.system.vo.ApplicationEntityVO;
import com.otitan.webapp.framework.basepro.model.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 应用表
 *
 * @author jiacs
 * @email xxx
 * @date 2020-09-15 14:51:37
 */
@Slf4j
@Api(tags = "应用表API")
@RestController
@RequestMapping("/application")
public class ApplicationController {

    @Autowired
    private ApplicationService applicationService;


    /**
    * 新增应用
    * @author Sunke
    * @param [application, bindingResult]
    * @return com.otitan.webapp.framework.basepro.model.DataResult
    * @exception
    * @date 2020/9/17 0017 14:32
    */
    @ApiOperation(value = "新增")
    @PostMapping()
    public DataResult add(@Valid @RequestBody ApplicationEntity application, BindingResult bindingResult) {
        return applicationService.add(application, bindingResult);
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    public DataResult batchDelete(@RequestBody @ApiParam(value = "ID集合") List<String> ids) {
        return applicationService.batchDelete(ids);
    }
    @ApiOperation(value = "删除")
    @DeleteMapping("{id}")
    public DataResult delete(@PathVariable("id") String id) {
        List<String> ids = new ArrayList<String>() {{
            add(id);
        }};
        return applicationService.batchDelete(ids);
    }

    @ApiOperation(value = "更新")
    @PutMapping
    public DataResult editor(@Valid @RequestBody ApplicationEntity application, BindingResult bindingResult) {
        return applicationService.editor(application, bindingResult);
    }

    @ApiOperation(value = "列表查询（分页）")
    @PostMapping("/{page}/{size}")
    public DataResult<IPage<ApplicationEntityVO>> findPageData(@PathVariable("page") long page,
                                                               @PathVariable("size") long size,
                                                               @RequestBody ApplicationDTO application) {
        return applicationService.findPageData(page, size, application);
    }

    @ApiOperation(value = "根据ID查询")
    @GetMapping("/{id}")
    public DataResult<ApplicationEntityVO> findOne(@PathVariable("id") String id) {
        return applicationService.findOne(id);
    }


    /**
    * 获取List
    * @author Sunke
    * @param
    * @return com.otitan.webapp.framework.basepro.model.DataResult
    * @exception
    * @date 2020/9/17 0017 18:10
    */
    @ApiOperation(value = "获取List")
    @GetMapping("/getAppList")
    public DataResult< List<ApplicationEntity>> getAppList() {
        return applicationService.getAppList();
    }

}
